package auntie.production.app2.svc;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.util.Log;
import auntie.production.app2.db.auntie_production_app_db;
import auntie.production.app2.db.auntie_production_app_db.production_details;
import auntie.production.app2.db.auntie_production_app_db.productions;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.android.apptools.OpenHelperManager.SqliteOpenHelperFactory;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;


public abstract class s1 extends OrmLiteBaseActivity<auntie_production_app_db> {

    private static final String LOG = s1.class.getName();
    public static final String s1_productions = "productions";
    
    static {
        OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {

            @Override
            public OrmLiteSqliteOpenHelper getHelper(Context ctx) {
                return new auntie_production_app_db(ctx);
            }
        });
    }

    protected void svc_test_production_details() {
        try {
//            Dao<production_details, Integer> pd_dao = getHelper().dao_production_details();
//            
//            production_details pd = new production_details(
//                    1, "productName", "sticker", 10, 10, 10, new Date() 
//                    );
//            pd_dao.create(pd);
            
           List<production_details> pd = svc_all_production_details_by_production(1);
            
            Log.i(LOG, pd.toString());
        } catch(Exception e) {
            Log.e(LOG, "Database exception ", e);
            return;
        }
    }
    
    protected void svc_testdb() {

        try {
            Dao<productions, Integer> p_dao = getHelper().dao_productions();

            productions p = new productions("Melvin", new Date());
            p_dao.create(p);

            Log.i(LOG, p.toString());

            Thread.sleep(2000);

            p.setInputer("Zamora");
            p.setModified(new Date());
            p_dao.update(p);

            Log.i(LOG, p.toString());

            p_dao.delete(p);

        } catch (Exception e) {
            Log.e(LOG, "Database exception ", e);
            return;

        }
    }

    protected productions svc_save_product(String name) {

        try {
            Dao<productions, Integer> p_dao = getHelper().dao_productions();

            productions p = new productions(name, new Date());
            p_dao.create(p);

            return p;

        } catch (Exception e) {
            Log.e(LOG, "Database exception ", e);
            return null;

        }
    }

    protected List<productions> svc_all_productions() {
        try {
            Dao<productions, Integer> p_dao = getHelper().dao_productions();

            PreparedQuery pq = p_dao.queryBuilder().where().eq("submitted", false).prepare();
            return p_dao.query(pq);
            
        } catch (Exception e) {
            Log.e(LOG, "Database exception ", e);
            return null;

        }
    }
    
    protected List<production_details> svc_all_production_details_by_production(int id) {
        try {
            Dao<production_details, Integer> pd_dao = getHelper().dao_production_details();
            PreparedQuery pq = pd_dao.queryBuilder().where().eq("production_id", id).prepare();
            Log.i(LOG, pq.toString());
            return pd_dao.query(pq) ;         
        } catch( Exception e) {
            Log.e(LOG, "Database exception", e);
            return null;
        }
    }
    
    protected void svc_update_production_details(production_details pd) {
        try {
            Dao<production_details, Integer> pd_dao = getHelper().dao_production_details();
            pd_dao.update(pd);
        } catch( Exception e) {
            Log.e(LOG, "Database exception", e);
        }
    }
    
    protected void svc_update_productions(productions p) {
        try {
            Dao<productions, Integer> p_dao = getHelper().dao_productions();
            p_dao.update(p);
        } catch( Exception e) {
            Log.e(LOG, "Database exception", e);
        }
    }
    
    protected void svc_save_production_details(production_details pd) {
        try {
            Dao<production_details, Integer> pd_dao = getHelper().dao_production_details();
            pd_dao.create(pd);
        } catch( Exception e) {
            Log.e(LOG, "Database exception", e);
        }
    }
    
}
